/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 조회 - Proxy
 * 				공지사항 조회하는 Model Logic 
 */
package com.skt.common_hr.alnotice.model
{
	import com.skt.common_hr.alnotice.AlnoticeFacade;
	import com.skt.common_hr.alnotice.model.vo.NoticeVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeViewProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeProxy";
		
		public var noticeList:ArrayCollection;
		
		public var T_HEGBN:ArrayCollection;
		public var T_MEDCK:ArrayCollection;
		public var T_RCODE:ArrayCollection;
		public var T_HCODE:ArrayCollection;
		public var T_SCODE:ArrayCollection;
		public var T_MEDCD:ArrayCollection;
		public var T_AMEDCD:ArrayCollection;
		
		
		public var rowCount:int;
		//public var vo:NoticeVO;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function NoticeViewProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadList(zyear:String=""):void
		{
			var param:Object = new Object();

		 	param["I_PERNR"] = Variables.myPernr;
			loadData("Z_HR_AN01", param, result1, fault1); 
		}
		
		//삭제
		public function deleteData(paramVo:Object):void
		{
			var params:Object = new Object();
			params["I_ACTIO"]		= "DEL";
			params["I_PERNR"]		= Variables.myPernr;
			params["I_HENUM"]		= paramVo.I_HENUM;
			loadData("Z_HR_HE02", params, updateResult, updateFault); 
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param target: 1:제목,2:작성자이름,3:내용
		 * @return void

		public function keywordSearch(target:String, keyWord:String):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]	= "01";
			param["I_RA_SEARCH"] = target; 
			param["I_SEARCH"]	= keyWord;
			param["I_MODE"]		= "M"; //관리자
			
			loadData("Z_HR_0032", param, result1, fault1); 
		}
		*/
		

		
		public function increaseCount(index:Number):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]		= "01";
			param["I_CONTENT_IDX"]		= index.toString();
			trace(index);
			loadData("Z_HR_0034", param, countResult, countFault); 
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		 
		public  var sRole:String="";
		
		public  var O_MEDCK:String="";
		public  var O_FMEDCK:String="";
		
		public  var O_ENAME:String="";
		public  var O_REGNO:String="";
		public  var O_RBEGDA:Date = new Date();
		public  var O_RENDDA:Date = new Date();
		
		public  var O_COMP:String="";
		public  var O_HOME:String="";
		
		// 신청 클릭했을때 medck가 없을때 띄워줄 메세지
		public  var reqErrMsg:String="";
		
		public function result1(event:ResultEvent):void
		{
			reqErrMsg = event.result.RETTEXT;
			if(retCode==0) {
				noticeList = parseNotice(event.result.OTAB);
				facade.sendNotification(AlnoticeFacade.LOAD_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(AlnoticeFacade.LOAD_NOTICE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault1(event:FaultEvent):void
		{
			facade.sendNotification(AlnoticeFacade.LOAD_NOTICE_FAILED, this);
		}
		
		
		public function updateResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(AlnoticeFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(AlnoticeFacade.UPDATE_NOTICE_FAILED, this);
			}
		}
		
		
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(AlnoticeFacade.UPDATE_NOTICE_FAILED, this);
		}
		
		public function countResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(AlnoticeFacade.UPDATE_COUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(AlnoticeFacade.UPDATE_COUNT_FAILED, this);
			}
		}
		
		
		private function countFault(event:FaultEvent):void
		{
			facade.sendNotification(AlnoticeFacade.UPDATE_COUNT_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseNotice(recArr:ArrayCollection):ArrayCollection
		{
			 var retArr:ArrayCollection = new ArrayCollection();

               for each(var obj:Object in recArr){
                    var retObj:NoticeVO = new NoticeVO(); 

                    for(var field:String in obj) {
                         if(retObj.hasOwnProperty(field)==false ) {
                              continue;
                         }
                                            
                         if( field == "BEGDA"  || field == "ENDDA" ) {
                              retObj[field+'_DATE'] = DateUtil.stringToDateTime(obj[field]);
                              retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DATE']);
                              retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DATE']);
                         }
                         else if(field == "CDATE") {                             
                              retObj[field+'_DATE'] = DateUtil.stringToDateTime(obj[field]);
                              retObj[field+'_TX'] 	= DateUtil.toDefaultString(retObj[field+'_DATE']) + " ";
                         }                                                        
                         else if(field == "CTIME") {                             
                              retObj[field+'_DATE'] = DateUtil.stringToDateTime(obj[field]);
                              retObj[field+'_TX'] 	= retObj[field+'_DATE'].getHours().toString()+":"+retObj[field+'_DATE'].getMinutes().toString();
                         }                                                                          
                         else {                             
                              retObj[field] = obj[field];
                         }
                    }
                    retArr.addItem(retObj);
               }
               return retArr;
		}
		
		private function parseCbVal(recArr:ArrayCollection, dv:String):ArrayCollection
		{
			 var retArr:ArrayCollection = new ArrayCollection();

				var retObj:Object = {};
               for each(var obj:Object in recArr){
					 
                    for(var field:String in obj) {
                         if(retObj.hasOwnProperty(field)==false ) {
                              continue;
                         }
                          retObj[field] = obj[field];
                    }
                    retArr.addItem(retObj);
               }
               return retArr;		
		}
		
	}
}